package mipo.top.admin.controller;

import mipo.top.admin.domain.Employee;
import mipo.top.admin.query.EmployeeQuery;
import mipo.top.admin.service.IEmployeeService;
import mipo.top.base.util.JsonResult;
import mipo.top.base.util.LoginContext;
import mipo.top.base.util.PageUtil;
import mipo.top.users.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @GetMapping
    public List<Employee> sad(){
        return employeeService.findall();
    }

    @PutMapping
    public JsonResult su(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                employeeService.save(employee);
            }else{
                employeeService.update(employee);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public JsonResult adw(@PathVariable("id") Long id){
        try {
            employeeService.remove(id);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
    }

    @PostMapping
    public PageUtil<Employee> asp(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryData(employeeQuery);
    }
    @PatchMapping
    private void bacthDel(@RequestBody List<Long> ids){
        employeeService.patchremove(ids);
    }

    @GetMapping("/findByshopId")
    private List<Employee> findByshopId(HttpServletRequest request){
        try {
            Logininfo loginUser = LoginContext.getLoginUser(request);
            Employee employee = employeeService.findBylogininfoId(loginUser.getId());
            return employeeService.findByshopId(employee.getShopId());
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
